﻿namespace MSSqlSchemaDoc.Provider.MicrosoftSqlServer.Helpers
{
    using System;
    using System.Data;
    using System.Globalization;

    /// <summary>
    /// Provides helper methods to ease the use of data reader objects.
    /// </summary>
    internal static class DataReaderHelper
    {
        /// <summary>
        /// Reads the value for a column containing a string value.
        /// </summary>
        /// <param name="reader">The open reader to read from.</param>
        /// <param name="columnName">The name of the column to get the data for.</param>
        /// <returns>The string contained in the column.</returns>
        public static string GetString(IDataReader reader, string columnName)
        {
            int ordinalPosition;
            try
            {
                ordinalPosition = reader.GetOrdinal(columnName);
            }
            catch (IndexOutOfRangeException)
            {
                return string.Empty;
            }

            // Can't use reader.GetString() because it will throw an error on NULL values
            object value = reader.GetValue(ordinalPosition);

            // Check for nulls
            if (value == null)
            {
                return null;
            }
            else
            {
                return value.ToString();
            }
        }

        /// <summary>
        /// Reads the value for a column containing a boolean value.
        /// </summary>
        /// <param name="reader">The open reader to read from.</param>
        /// <param name="columnName">The name of the column to get the data for.</param>
        /// <returns>The boolean value from the column.</returns>
        public static bool GetBool(IDataReader reader, string columnName)
        {
            int ordinalPosition;
            try
            {
                ordinalPosition = reader.GetOrdinal(columnName);
            }
            catch (IndexOutOfRangeException)
            {
                return false;
            }

            object value = reader.GetValue(ordinalPosition);
            if (value == null)
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(value, CultureInfo.InvariantCulture);
            }
        }

        /// <summary>
        /// Reads the value for a column containing an integer value.
        /// </summary>
        /// <param name="reader">The open reader to read from.</param>
        /// <param name="columnName">The name of the column to get the data for.</param>
        /// <returns>The integer value from the column.</returns>
        public static int? GetInt(IDataReader reader, string columnName)
        {
            object value = reader.GetValue(reader.GetOrdinal(columnName));
            if (value == DBNull.Value)
            {
                return null;
            }
            else
            {
                return Convert.ToInt32(reader.GetValue(reader.GetOrdinal(columnName)), CultureInfo.InvariantCulture);
            }
        }
    }
}
